package com.shanghaionstar.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.EncryptUtil;
import com.shanghaionstar.R;
import com.shanghaionstar.utils.H5JSObject;

/**
 *
 * 驾驶行为服务协议
 *
 * Created by Yonglang Hong on 2016/9/2.
 */
public class AgreementDialog extends Dialog{

    View btnConfirm,btnCancel;

    ProgressBar progress_bar;

    WebView webView;

    private String agreementLink;

    public void setAgreementLink(String agreementLink) {
        this.agreementLink = agreementLink;
    }

    public AgreementDialog(Context context) {
        super(context);
        init(context);
    }

    public AgreementDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    public AgreementDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context){

        //http://idt1.onstar.com.cn/mweb1/mweb-main/static/OnStar-MA7.0-h5/html/car-assessment/SmartDriver-agreement.html
        agreementLink = Constants.CURRENT_PREFIX_H5_URL + Constants.DRIVE_AGREEMENT_ZH;

        View view = View.inflate(context, R.layout.smartdriver_dialog2, null);

        webView = (WebView) view.findViewById(R.id.web_view);
        btnCancel  = view.findViewById(R.id.btn_denial);
        btnConfirm = view.findViewById(R.id.btn_agree);
        progress_bar = (ProgressBar) view.findViewById(R.id.progress_bar);

        webView.addJavascriptInterface(new H5JSObject(context), "jsonObject");
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showProgressDialog();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dimissProgressDialog();
            }
        });

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(view);
    }

    private void showProgressDialog(){
        progress_bar.setVisibility(View.VISIBLE);
    }

    private void dimissProgressDialog(){
        progress_bar.setVisibility(View.GONE);
    }

    @Override
    public void show() {
        if (webView != null)
            webView.loadUrl(agreementLink);
        Window window = getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        DisplayMetrics metrics = new DisplayMetrics();
        d.getMetrics(metrics);
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        LogManager.getInstance().d("widthPixels" + metrics.widthPixels);
        p.width = (int) (metrics.widthPixels * 0.85);
        p.height = (int) (metrics.heightPixels * 0.7);
        window.setAttributes(p);
        super.show();
    }

    public void setConfirmListener(View.OnClickListener listener){
        btnConfirm.setOnClickListener(listener);
    }

    public void setCancelListener(View.OnClickListener listener){
        btnCancel.setOnClickListener(listener);
    }

}
